package _interview150;

import _object.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * 114. 二叉树展开为链表
 */
public class No114 {
    private List<TreeNode> list;

    public void flatten(TreeNode root) {
        if (root == null) return;
        list = new ArrayList<>();
        dfs(root);
        TreeNode pre = list.get(0);
        for (int i = 1; i < list.size(); i++) {
            pre.left = null;
            pre.right = list.get(i);
            pre = pre.right;
        }
    }

    private void dfs(TreeNode root) {
        if (root == null) return;
        list.add(root);
        dfs(root.left);
        dfs(root.right);
    }
}
